<!-- 结算价&变动成本公告表编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改结算价&变动成本公告表':'添加结算价&变动成本公告表'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="82px">
        <el-form-item
          label="采购方编码:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.purchaseCode"
            placeholder="请输入采购方编码"
            clearable/>
        </el-form-item>
        <el-form-item
          label="销售方编码:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.saleCode"
            placeholder="请输入销售方编码"
            clearable/>
        </el-form-item>
        <el-form-item
          label="物料编码:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.materialCode"
            placeholder="请输入物料编码"
            clearable/>
        </el-form-item>
        <el-form-item
          label="期间（月份）:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.period"
            placeholder="请输入期间（月份）"
            clearable/>
        </el-form-item>
        <el-form-item
          label="年度:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.yearly"
            placeholder="请输入年度"
            clearable/>
        </el-form-item>
        <el-form-item
          label="数据来源:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.dataSource"
            placeholder="请输入数据来源"
            clearable/>
        </el-form-item>
        <el-form-item
          label="BIP主键:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.bipId"
            placeholder="请输入BIP主键"
            clearable/>
        </el-form-item>
        <el-form-item
          label="创建人:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.creator"
            placeholder="请输入创建人"
            clearable/>
        </el-form-item>
        <el-form-item
          label="修改人:"
          prop="name">
          <el-input
            :maxlength="20"
            v-model="form.modifier"
            placeholder="请输入修改人"
            clearable/>
        </el-form-item>
        <el-form-item label="修改时间:">
          <el-date-picker
            type="datetime"
            class="ele-fluid"
            v-model="form.modifyTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择修改时间"/>
        </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'PriceVariNoticeEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        purchaseCode: [
          {required: true, message: '请输入采购方编码', trigger: 'blur'}
        ],
        saleCode: [
          {required: true, message: '请输入销售方编码', trigger: 'blur'}
        ],
        materialCode: [
          {required: true, message: '请输入物料编码', trigger: 'blur'}
        ],
        assertPrice: [
          {required: true, message: '请输入考核结算价', trigger: 'blur'}
        ],
        taxRaisePrice: [
          {required: true, message: '请输入税筹结算价', trigger: 'blur'}
        ],
        budgetVariablePrice: [
          {required: true, message: '请输入预算变动成本', trigger: 'blur'}
        ],
        period: [
          {required: true, message: '请输入期间（月份）', trigger: 'blur'}
        ],
        yearly: [
          {required: true, message: '请输入年度', trigger: 'blur'}
        ],
        dataSource: [
          {required: true, message: '请输入数据来源', trigger: 'blur'}
        ],
        bipId: [
          {required: true, message: '请输入BIP主键', trigger: 'blur'}
        ],
        creator: [
          {required: true, message: '请输入创建人', trigger: 'blur'}
        ],
        modifier: [
          {required: true, message: '请输入修改人', trigger: 'blur'}
        ],
        modifyTime: [
          {required: true, message: '请输入修改时间', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/pricevarinotice/edit' : '/pricevarinotice/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
